package net.example.myapplication;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;
import android.widget.TextView;

import com.googlecode.tesseract.android.TessBaseAPI;

import java.io.File;

public class MainActivity extends AppCompatActivity {

    TessBaseAPI mTess=null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
                MainActivity.this.onClick();
            }
        });
        initTessBaseData();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }


    private void initTessBaseData() {

        new File("/storage/sdcard1").exists();
        mTess = new TessBaseAPI();
        Environment.getDataDirectory();
        String datapath = Environment.getExternalStorageDirectory() + "/tesseract/";
        datapath="/storage/sdcard1/";

        // String language = "num";

        String language = "chi_sim";

        File dir = new File(datapath + "tessdata/");

        if (!dir.exists())

            dir.mkdirs();
        File[] files=dir.listFiles();
        for (int i = 0; i < files.length; i++) {
            File file = files[i];
            Log.d(MainActivity.this.getPackageName(),file.getPath());
        }

        mTess.init(datapath, language);

    }

    public void onClick() {

//        Bitmap bitmap = BitmapFactory.decodeResource(this.getResources(), R.drawable.textimage);
        Bitmap bitmap = BitmapFactory.decodeResource(this.getResources(), R.drawable.timg_1);
        mTess.setImage(bitmap);
        String result = mTess.getUTF8Text();

        TextView txtView = (TextView)this.findViewById(R.id.idCard_textView);
        Log.d(MainActivity.class.getPackage().getName(),"result: "+result);
        txtView.setText("结果为:" + result);

        ImageView imgView = (ImageView)this.findViewById(R.id.imageView);

        imgView.setImageBitmap(bitmap);

    }
}
